<?php
/**
 * 频道焦点图 适用于产品（D/E/G）
 * 特殊说明：F为G和独家策划拼接而成
 * 使用说明：需要传递自动参数  6个参数
 * limit
 * level
 * topTagId
 * tagIds(格式：格式1,格式2,格式3)
 * queryType(查询方式 预留参数)默认1
 * showType (显示方式 1=>D 2=>E 3=>G)
 * ChannelFocusController.php
 * Create on 2015-04-14 17:48:38
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\channel;

use haibao\cms\business\control\ArticleController;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;

class ChannelFocusController extends \haibao\cms\business\control\pc\channel\ChannelController{
	

	const SHOW_TYPE_ONE_D = 1;
	const SHOW_TYPE_TWO_E = 2;
	const SHOW_TYPE_THREE_G = 3;
	
	public function __construct($updateTime = null,$controlId,$isPublish){
	    $this->savaLog();
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		if($this->checkData($controlData)){
			
			$this->parseTagId($controlData['manual']['tagIds'],$controlData['manual']['topTagId']);
			$this->getFirstActualData($controlData['manual']);
			$this->assign('starsUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_STARS));
			$this->assign('lifeUrl', \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_LIFE).'/astro');
			$path = $this->generateHtmlPath(__CLASS__);
			$isNewHtml = $this->generateHtml($path,$content);
			$content = $this->getHtml($path);
			$content = $this->replaceCDNLink($content,true);
		}else{
			return $content = '<div></div>';
		}
		return $content;
	}
	
	/**
	 * @param : $dataCondition 数据条件
	 */
	public function getFirstActualData($dataCondition){

		$dataCondition['source'] = __CLASS__;
		if($dataCondition['showType'] == self::SHOW_TYPE_ONE_D){
			$dataCondition['isProImage'] = true;
			$dataCondition['isTag'] = true;
			$dataCondition['isReturnTag'] = true;
			$dataCondition['isParentTag'] = false;
			$dataCondition['isPrimaryTag'] = false;
			$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_SIX);
		}elseif($dataCondition['showType'] == self::SHOW_TYPE_TWO_E){
			$dataCondition['isProImage'] = true;
			$dataCondition['isTag'] = true;
			$dataCondition['isReturnTag'] = true;
			$dataCondition['isParentTag'] = false;
			$dataCondition['isPrimaryTag'] = false;
			$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_SIX,\haibao\cms\model\data\ArticleProImage::Level_FIVE,\haibao\cms\model\data\ArticleProImage::Level_FOUR);
		}elseif($dataCondition['showType'] == self::SHOW_TYPE_THREE_G){
			$dataCondition['isProImage'] = true;
			$dataCondition['isTag'] = true;
			$dataCondition['isReturnTag'] = true;
			$dataCondition['isParentTag'] = false;
			$dataCondition['isPrimaryTag'] = false;
			$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
		}
		
		list($articles,$imageColls) = $this->getArticleControl(1,$dataCondition);
		
		$mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
		if($dataCondition['showType'] == self::SHOW_TYPE_ONE_D){
			$width = 360;
			$height = 480;
			if(isset($dataCondition['width'])) $width = $dataCondition['width'];
			if(isset($dataCondition['height'])) $height = $dataCondition['height'];
			$this->packageChannelD($mergeData,$articles,$imageColls,$width,$height);
		}elseif($dataCondition['showType'] == self::SHOW_TYPE_TWO_E){

			$this->packageChannelE($mergeData,$articles,$imageColls);
		}elseif($dataCondition['showType'] == self::SHOW_TYPE_THREE_G){

			$this->packageChannelG($mergeData,$articles,$imageColls);
		}
	}
	
	/**
	 * 封装频道风格为D的数据
	 * @param Array $mergeData 合并结果集数据
	 * @param Array $articles 文章资讯数据
	 * @param Array $imageColls 图集资讯数据
	 */
	private function packageChannelD($mergeData,$articles,$imageColls,$width,$height){
		$bakArr = array(5=>'',1=>'',2=>'',3=>'',4=>'');
		$dataArr = array();
		
		foreach($mergeData as $key=>$article){
			$key = $key+1;
			$dataArr[$key] = $article->toArray();
			$dataArr[$key]['SiteType'] = $article->SiteType;
			if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){
				$tag = $articles['tag'];
				$articleTag = $articles['articleTag'];
				
				$dataArr[$key]['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
				$dataArr[$key]['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
				
				$dataArr[$key]['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
				$dataArr[$key]['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
				
				if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX])){
					$dataArr[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX];
				}else{
					if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
						$dataArr[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
					}else{
						$dataArr[$key]['ProImage'] = \haibao\cms\business\control\PublicController::getArticleFirstImage($article->Id);
					}
				}
				if(isset($dataArr[$key]['ProImage'])){
					
					$dataArr[$key]['ImageUrl'] = self::getImageLink($dataArr[$key]['ProImage'],$width,$height);
				}
			}else{
				$tag = $imageColls['tag'];
				$collTag = $imageColls['collTag'];
				$dataArr[$key]['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
				$dataArr[$key]['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
				$dataArr[$key]['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
				$dataArr[$key]['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];

				if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX])){
					$dataArr[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX];
				}else{
					if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
						$dataArr[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
					}else{
						$dataArr[$key]['ProImage'] = '';
					}
				}
				if(isset($dataArr[$key]['ProImage'])){
						
					$dataArr[$key]['ImageUrl'] = self::getImageLink($dataArr[$key]['ProImage'],$width,$height);
				}
			}
		}
		
		$finalArr = array();
		if($dataArr){
			foreach($bakArr as $key=>$arr){
				if(isset($dataArr[$key])){
					$finalArr[$key] = $dataArr[$key];
				}
			}
		}
		$this->assign('finalArr', $finalArr);
		$this->assign('tagInfo', PublicControllerBusiness::getArticlePrimaryTagByTagId($this->conditionTagId));
		$this->assign('picHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));
		
	}
	
	/**
	 * 封装频道风格为E的数据
	 * @param Array $mergeData 合并结果集数据
	 * @param Array $articles 文章资讯数据
	 * @param Array $imageColls 图集资讯数据
	 */
	private function packageChannelE($mergeData,$articles,$imageColls){
		$leftArticle = $centerArticle = $rightArticle = array();
		foreach($mergeData as $key=>$article){
			switch($key){
				case 0:
					$leftArticle['Id'] = $article->Id;
					$leftArticle['Title'] = $article->Title;
					$leftArticle['ShortTitle'] = $article->ShortTitle;
					$leftArticle['SiteType'] = $article->SiteType;
					if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){

						$tag = $articles['tag'];
						$articleTag = $articles['articleTag'];
						
						$leftArticle['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
						$leftArticle['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
						$leftArticle['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
		
						if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX])){
							$leftArticle['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX];
						}else{
							if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
								$leftArticle['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
							}else{
								$leftArticle['ProImage'] = '';
							}
						}
					}else{

						$tag = $imageColls['tag'];
						$collTag = $imageColls['collTag'];
						
						$leftArticle['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
						$leftArticle['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
						$leftArticle['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
		
						if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX])){
							$leftArticle['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX];
						}else{
							if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
								$leftArticle['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
							}else{
								$leftArticle['ProImage'] = '';
							}
						}
					}
					$this->assign('leftArticle',$leftArticle);
					break;
				case 1:
				case 2:
					$centerArticle[$key] = $article->toArray();
					$centerArticle[$key]['SiteType'] = $article->SiteType;
					if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){

						$tag = $articles['tag'];
						$articleTag = $articles['articleTag'];
						
						$centerArticle[$key]['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
						$centerArticle[$key]['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
						$centerArticle[$key]['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
						$centerArticle[$key]['Title'] = $article->Title;
						
						if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])){
							$centerArticle[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE];
						}else{
							if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
								$centerArticle[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
							}else{
								$centerArticle[$key]['ProImage'] = '';
							}
						}
					}else{

						$tag = $imageColls['tag'];
						$collTag = $imageColls['collTag'];
						$centerArticle[$key]['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
						$centerArticle[$key]['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
						$centerArticle[$key]['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
						$centerArticle[$key]['Title'] = $article->Title;
						
						if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])){
							$centerArticle[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE];
						}else{
							if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
								$centerArticle[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
							}else{
								$centerArticle[$key]['ProImage'] = '';
							}
						}
					}
					break;
				case 3:
				case 4:
				case 5:
					$rightArticle[$key] = $article->toArray();
					if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){
						
						$tag = $articles['tag'];
						$articleTag = $articles['articleTag'];
						
						$rightArticle[$key]['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
						$rightArticle[$key]['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
						
						
						$rightArticle[$key]['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
						$rightArticle[$key]['Title'] = $article->Title;
						$rightArticle[$key]['SiteType'] = $article->SiteType;
						
						if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
							$rightArticle[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
						}else{
							if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
								$rightArticle[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
							}else{
								$rightArticle[$key]['ProImage'] = '';
							}
						}
					}else{
						$tag = $imageColls['tag'];
						$collTag = $imageColls['collTag'];
						
						$rightArticle[$key]['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
						$rightArticle[$key]['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
						

						$rightArticle[$key]['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
						$rightArticle[$key]['Title'] = $article->Title;
						$rightArticle[$key]['SiteType'] = $article->SiteType;
											
						if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
							$rightArticle[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
						}else{
							if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
								$rightArticle[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
							}else{
								$rightArticle[$key]['ProImage'] = '';
							}
						}
					}
						break;
				default:
					break;
			}
		}
		$this->assign('centerArticle',json_decode(json_encode(array_values($centerArticle))));
		$this->assign('rightArticle',json_decode(json_encode(array_values($rightArticle))));
		
		$this->assign('tagInfo', PublicControllerBusiness::getArticlePrimaryTagByTagId($this->conditionTagId));
		$this->assign('picHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));
	}
	
	/**
	 * 封装频道风格为G的数据
	 * @param Array $mergeData 合并结果集数据
	 * @param Array $articles 文章资讯数据
	 * @param Array $imageColls 图集资讯数据
	 */
	private function packageChannelG($mergeData,$articles,$imageColls){
		
		$bakArr = array(4=>'',5=>'',1=>'',2=>'',3=>'');
		$dataArr = array();
	
		foreach($mergeData as $key=>$article){
			$key = $key+1;
			$dataArr[$key] = $article->toArray();

			
			if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){
				
				$tag = $articles['tag'];
				$articleTag = $articles['articleTag'];
				$dataArr[$key]['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
				$dataArr[$key]['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
				$dataArr[$key]['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
				
				if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
					$dataArr[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
				}else{
					if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
						$dataArr[$key]['ProImage'] = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
					}else{
						$dataArr[$key]['ProImage'] = '';
					}
				}
				$dataArr[$key]['SiteType'] = $article->SiteType;
				$dataArr[$key]['Title'] = $article->Title;
				$dataArr[$key]['Id'] = $article->Id;
				$dataArr[$key]['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
			}else{
				
				$tag = $imageColls['tag'];
				$collTag = $imageColls['collTag'];
				
				$dataArr[$key]['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
				$dataArr[$key]['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
				$dataArr[$key]['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
				if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
					$dataArr[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
				}else{
					if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
						$dataArr[$key]['ProImage'] = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
					}else{
						$dataArr[$key]['ProImage'] = '';
					}
				}
				$dataArr[$key]['SiteType'] = $article->SiteType;
				$dataArr[$key]['Title'] = $article->Title;
				$dataArr[$key]['Id'] = $article->Id;
				$dataArr[$key]['PublishTime'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
			}
		}
	
		$finalArr = array();
		if($dataArr){
			foreach($bakArr as $key=>$arr){
				$finalArr[$key] = $dataArr[$key];
			}
		}
		$this->assign('finalArr',$finalArr);
		$this->assign('articleList',array_slice($dataArr, 5,4));
		$this->assign('picHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));
	
	}
	private function checkData($controlData){
		if($controlData['manual']){
				
			if(isset($controlData['manual']['limit']) &&
			isset($controlData['manual']['level']) &&
			isset($controlData['manual']['topTagId']) &&
			isset($controlData['manual']['tagIds']) &&
			isset($controlData['manual']['showType']) 
				
			){
				return true;
	
			}else{
				return false;
			}
		}
	}
}